package com.gis.servlet.find;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.gis.bean.Point;
import com.gis.dao.FindDao;
import com.gis.dto.FindDto;
import com.gis.util.GisUtil;
import com.gis.util.PageResult;
import com.gis.util.ResponseUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author t-zhaoheng
 * @since 2022/5/16
 */
@WebServlet("/find/page")
public class FindPageServlet extends HttpServlet {
  FindDao findDao = FindDao.getInstance();

  /**
   *
   */
  private static final long serialVersionUID = 1L;

  @Override
  protected void doGet(
      HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(req, resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Map<String, Object> params = new HashMap<>();
    String username = "";
    if(req.getParameter("username") != null) {
      username = req.getParameter("username");
      params.put("username", username);
    } else {
      HttpSession session = req.getSession();
      Object user = session.getAttribute("username");
      if (user == null) {
        ResponseUtil.writePage(resp, null);
        return;
      } else {
        username = (String) user;
        params.put("username", username);
      }
    }
    // easyUI分页时 默认使用的参数是 page/rows 来表示页码和行数 而返回值也必须使用total/rows 其中total表示总行数 而rows表示返回集合数组
    String pageSize = req.getParameter("rows");
    if(pageSize == null) {
      pageSize = "5";
    }
    String pageNum = req.getParameter("page");
    if(pageNum == null) {
      pageNum = "1";
    }

    String goodsTypeId = req.getParameter("goodsTypeId");
    if (StrUtil.isNotBlank(goodsTypeId)) {
      params.put("goodsTypeId", goodsTypeId);
    }
    String startDate = req.getParameter("startDate");
    if (StrUtil.isNotBlank(startDate)) {
      params.put("startDate", startDate);
    }
    String endDate = req.getParameter("endDate");
    if(StrUtil.isNotBlank(endDate)) {
      params.put("endDate", endDate);
    }
    String points = req.getParameter("points");
    List<Point> pointList = new ArrayList<>();
    List<Double> lonList = new ArrayList<>();
    List<Double> latList = new ArrayList<>();
    if(StrUtil.isNotBlank(points)) {
      pointList = JSONArray.parseArray(points, Point.class);
    }
    for (Point point : pointList) {
      lonList.add(point.getLng());
      latList.add(point.getLat());
    }
    Double[] lons = Convert.toDoubleArray(lonList);
    Double[] lats = Convert.toDoubleArray(latList);

    Integer size = Integer.parseInt(pageSize);
    Integer num = Integer.parseInt(pageNum);
    params.put("offset", (num - 1) * size);
    params.put("rows", size);
    PageResult<FindDto> pageResult = findDao.page(params);
    // 再按照前端选取的经纬度过滤
    if(CollectionUtil.isNotEmpty(pointList)) {
      List<FindDto> collect = pageResult.getRows().stream().filter(findDto -> {
        // 保证返回的经纬度位于选取的点中
        Double lng = findDto.getLng();
        Double lat = findDto.getLat();

        boolean b = GisUtil.isInPolygon(lng, lat, lons, lats);
        return b;
      }).collect(Collectors.toList());
      pageResult.setRows(collect);
      pageResult.setTotal((long) collect.size());
    }

    ResponseUtil.writePage(resp, pageResult);
  }
}
